package com.jiangjiang.eshop.comment.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.comment.constant.*;
import com.jiangjiang.eshop.comment.dto.*;
import com.jiangjiang.eshop.comment.service.CommentInfoService;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName CommentInfoBiz
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/13 15:24
 * @Version 1.0
 **/
@Service
public class CommentInfoBiz {
    @Autowired
    private CommentInfoService commentInfoService;
    @Autowired
    private CommentAggregateBiz aggregateBiz;


    /**
     * 新增评论（手动发表）
     *
     * @param commentInfoDTO
     */
    public void saveManualPublishedCommentInfo(CommentInfoDTO commentInfoDTO) {
        // 数据补充
        Integer totalScore = Math.round((commentInfoDTO.getGoodsScore()
                + commentInfoDTO.getCustomerServiceScore()
                + commentInfoDTO.getLogisticsScore()) / 3);

        // 设置总分
        commentInfoDTO.setTotalScore(totalScore);
        // 设置是否是默认评论
        commentInfoDTO.setDefaultComment(DefaultComment.NO);
        // 设置评论状态
        commentInfoDTO.setCommentStatus(CommentStatus.APPROVING);

        Integer commentType = 0;
        if (totalScore >= CommentInfoScore.FORE) {
            commentType = CommentType.GOOD_COMMENT;
        }
        if (totalScore.equals(CommentInfoScore.THREE)) {
            commentType = CommentType.MEDIUM_COMMENT;
        }
        if (totalScore > CommentInfoScore.ZERO && totalScore <= CommentInfoScore.TWO) {
            commentType = CommentType.BAD_COMMENT;
        }
        commentInfoDTO.setCommentType(commentType);

        Long commentInfoId = commentInfoService.saveCommentInfo(commentInfoDTO);
        // 补充评论id
        commentInfoDTO.setId(commentInfoId);
    }

    /**
     * 自动发表评论
     *
     * @param orderInfoDTO
     * @param orderItem
     * @return
     */
    public CommentInfoDTO saveAutoPublishCommentInfo(OrderInfoDTO orderInfoDTO, OrderItemDTO orderItem) {

        CommentInfoDTO dto = convertCommentInfoDTO(orderInfoDTO, orderItem);
        Long id = commentInfoService.saveCommentInfo(dto);
        dto.setId(id);
        return dto;
    }

    private CommentInfoDTO convertCommentInfoDTO(OrderInfoDTO orderInfoDTO, OrderItemDTO orderItem) {
        return CommentInfoDTO.builder()
                .userAccountId(orderInfoDTO.getUserAccountId())
                .username(orderInfoDTO.getUsername())
                .orderInfoId(orderInfoDTO.getId())
                .orderItemId(orderItem.getId())
                .goodsId(orderItem.getGoodsId())
                .goodsSkuId(orderItem.getGoodsSkuId())
                .totalScore(CommentInfoScore.FIVE)
                .goodsScore(CommentInfoScore.FIVE)
                .customerServiceScore(CommentInfoScore.FIVE)
                .logisticsScore(CommentInfoScore.FIVE)
                .commentContent("默认评论")
                .showPictures(ShowPictures.NO)
                .defaultComment(DefaultComment.YES)
                .commentStatus(CommentStatus.APPROVED)
                .commentType(CommentType.GOOD_COMMENT)
                .gmtCreate(new Date())
                .gmtModified(new Date())
                .build();
    }


    /**
     * 评论分页列表
     *
     * @param dto
     * @return
     */
    public Page<CommentInfoDTO> queryForPage(CommentPageQueryDTO dto) {
        return commentInfoService.queryForPage(dto);
    }

    /**
     * 根据id查询评论详情
     *
     * @param id
     * @return
     */
    public CommentDetailDTO queryById(Long id) {
        return commentInfoService.queryById(id);
    }

    /**
     * 审核评论
     *
     * @param id
     * @param approved
     * @return
     */
    public Boolean approveComment(Long id, Integer approved) {
        return commentInfoService.approveComment(id, approved);
    }

    /**
     * 删除评论
     *
     * @param id 评论id
     * @return 处理结果
     */
    public Boolean deleteComment(Long id) {
        return commentInfoService.removeById(id);
    }

    /**
     * 分页查询评论前台列表
     *
     * @param dto 评论查询条件
     * @return 查询结果
     */
    public CommentShowDTO queryCommentShow(CommentPageQueryDTO dto) {
        return CommentShowDTO.builder()
                .aggregate(aggregateBiz.queryByGoodsId(dto.getGoodsId()))
                .commentInfoDTOList( this.queryForPage(dto))
                .build();
    }

}
